package cn.yudao.ymcc.web.controller;

import cn.yudao.ymcc.domain.MessageEmail;
import cn.yudao.ymcc.service.IMessageBlacklistService;
import cn.yudao.ymcc.domain.MessageBlacklist;
import cn.yudao.ymcc.query.MessageBlacklistQuery;
import cn.yudao.ymcc.result.JSONResult;
import cn.yudao.ymcc.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import net.sf.jsqlparser.expression.LongValue;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/messageBlacklist")
public class MessageBlacklistController {

    @Autowired
    public IMessageBlacklistService messageBlacklistService;

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody MessageBlacklist messageBlacklist){
        if(messageBlacklist.getId()!=null){
            messageBlacklistService.updateById(messageBlacklist);
        }else{
            messageBlacklistService.insert(messageBlacklist);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        messageBlacklistService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(messageBlacklistService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(messageBlacklistService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody MessageBlacklistQuery query){
        // 从redis中获取
//        Integer page1 = query.getPage();
//        Integer rows = query.getRows();
//        Integer fromIndex = page1 * rows - rows;
//        Integer toIndex = page1 * rows;
//        List<MessageBlacklist> black = new ArrayList<>();
//        if (StringUtils.isEmpty(query.getKeyword())){
//            black = (List<MessageBlacklist>)redisTemplate.opsForValue().get("black");
//        }else {
//            MessageBlacklist blacklist = (MessageBlacklist)redisTemplate.opsForValue().get(query.getKeyword());
//            black.add(blacklist);
//        }
//        List<MessageBlacklist> pageList = new ArrayList<>();
//        if (toIndex > black.size()){
//             pageList = black.subList(fromIndex, black.size());
//        }else {
//             pageList = black.subList(fromIndex, toIndex);
//        }
//
//        int sizea = pageList.size();
//        Long size = Long.valueOf(sizea);
//        return JSONResult.success(new PageList<MessageBlacklist>(size,pageList));

        // 查询数据库
        Page<MessageBlacklist> page = new Page<MessageBlacklist>(query.getPage(),query.getRows());
        page = messageBlacklistService.selectPage(page);
        return JSONResult.success(new PageList<MessageBlacklist>(page.getTotal(),page.getRecords()));
    }
}
